package com.yami.shop.admin.scheduledtasks;

import com.yami.shop.admin.config.SpringConfig;
import com.yami.shop.bean.model.User;
import com.yami.shop.otherservice.SendSmsService;
import com.yami.shop.otherservice.UserLevelUpgradeService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户等级自动执行
 */
@Component("userLevelTask")
public class UserLevelTask {
    @Autowired
    private UserLevelUpgradeService userLevelUpgradeService;
    @Autowired
    private SpringConfig springConfig;
    @Autowired
    private SendSmsService sendSmsService;


    //每天凌晨二分停用失效的用户等级
    @Scheduled(cron = "0 2 0 * * ? ")
    public void deactivateExpireMember(){
        userLevelUpgradeService.deactivateExpireMember();
    }

    //每天八点发送会员还有7天到期短信
    @Scheduled(cron = "0 0 8 * * ? ")
    public void getSevenDayExpireMember(){
        boolean prod = StringUtil.equals(springConfig.activeProfiles, "prod");
        if(!prod){
            //只在正式环境发短信
            return;
        }
        List<User> sevenDayExpireMember = userLevelUpgradeService.getSevenDayExpireMember();
        //发送短信
        Map<String,String> map=new HashMap<>();
        map.put("days","7");
        for(User user:sevenDayExpireMember){
            sendSmsService.sendSms(user.getUserMobile(),"SMS_472690036",map);
        }
    }
}
